package com.elvin.wx.controller;


import com.elvin.wx.beans.*;
import com.elvin.wx.service.WarmService;
import com.elvin.wx.service.WireDevDeviceService;
import com.elvin.wx.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 无线设备表 前端控制器
 * </p>
 *
 * @author zhenchl
 * @since 2022-01-14
 */
@Controller
@RestController
@RequestMapping("/wireDevBase")
@Log4j2
@Api("无线设备管理")
public class WireDevBaseController {

    @Resource
    private WireDevDeviceService service;

    @Resource
    private WarmService warmService;

    /*@ApiOperation(value = "获取所有无线设备数据")
    @PostMapping("/queryAll")
    public Result<Object> queryAll() {
        Result<Object> result = new Result<>();
        try {
            List<WireDevBase> selectList = service.selectList(null);
            result.success(selectList);
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }*/

    @ApiOperation("多条件动态分页查询所有信息")
    @PostMapping("/queryAll")
    public Object queryAll( @ApiParam("设备编码") @RequestParam(value = "deviceCode", required = false) String deviceCode,
                                      @ApiParam("设备名称") @RequestParam(value = "devTitle", required = false) String devTitle,
                                      @ApiParam("设备类型") @RequestParam(value = "deviceType", required = false) String deviceType,
                                      @ApiParam("设备型号") @RequestParam(value = "modelNumber", required = false) String modelNumber,
                                      @ApiParam("组织机构id") @RequestParam(value = "organizeId", required = false) String organizeCode,
                                      @ApiParam(name = "pageNumber",value = "当前页码",required=true) @RequestParam("pageNumber") int page,
                                      @ApiParam(name = "pageSize",value = "当前页显示条数",required=true) @RequestParam("pageSize") int limit
                                      ) {
            WireDevBase wireDevBase = new WireDevBase();
            wireDevBase.setDeviceCode(deviceCode);
            wireDevBase.setDevTitle(devTitle);
            wireDevBase.setDeviceType(deviceType);
            wireDevBase.setModelNumber(modelNumber);
            wireDevBase.setOrganizeId(organizeCode);

            PageVo pageVo = new PageVo();
            pageVo.setPage(page);
            pageVo.setPageSize(limit);
            wireDevBase.setPageVo(pageVo);
            int total = service.selectPageCount(wireDevBase);
            Map<String, Object> map = new HashMap<String, Object>();
            if(total>0){
                List<WireDevBase> list = service.selectForPage(wireDevBase);
                map.put("data", list);
            }else{
                map.put("data", null);
            }
            map.put("status", 0);
            map.put("total", total);
            map.put("message", "查询成功");
            return map;
    }

    @ApiOperation(value = "新增无线设备数据")
    @PostMapping("/insert")
    public Result<Object> insert(@RequestBody WireDevBase entity) {
        Result<Object> result = new Result<>();
        try {
            service.insert(entity);
            result.success(entity, "新增成功");
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "修改无线设备数据")
    @PostMapping("/update")
    public Result<Object> update(@RequestBody WireDevBase entity) {
        Result<Object> result = new Result<>();
        try {
            service.updateById(entity);
            result.success("修改成功");
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "删除无线设备数据 ")
    @PostMapping("/delete")
    public Result<Object> delete(@ApiParam("无线设备数据id") @RequestParam("id") Long id) {
        Result<Object> result = new Result<>();
        try {
            service.deleteById(id);
            result.success("删除成功");
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据ID获取无线设备数据")
    @PostMapping("/getById")
    public Result<Object> getById(@ApiParam("无线设备数据id") @RequestParam("id") Long id) {
        Result<Object> result = new Result<>();
        try {
            WireDevBase entity = new WireDevBase();
            entity.setId(id);
            entity = service.selectById(entity);
            result.success(entity);
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }
    /*@ApiOperation(value = "根据条件修改无线设备数据")
    @PostMapping("/updateBy")
    public Result<Object> updateBy(@RequestBody List<WireDevParam> current_data) {
        Result<Object> result = new Result<>();
        try {
             WireDevBase entity = new WireDevBase();
            for (WireDevParam param: current_data) {
                entity.setProId(param.getUser_id());
                entity.setDevId(param.getDev_id());
                entity.setcDeviceId(param.getAt());
                entity.setdDeviceId(param.getValue());
                service.updateBy(entity);
            }
            result.success("修改成功");
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }*/
    @ApiOperation(value = "根据条件修改无线设备数据")
    @PostMapping("/updateBy")
    public Result<Object> updateBy(@RequestBody WireDevParamNew current_data) {
        Result<Object> result = new Result<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            WireDevBase entity = new WireDevBase();
            List<WireDevParam> current_data1 = current_data.getCurrent_data();
            for (WireDevParam param: current_data1) {
                entity.setProId(param.getUser_id());
                entity.setDevId(param.getDev_id());
                entity.setcDeviceId(param.getAt());
                entity.setdDeviceId(param.getValue());
                service.updateBy(entity);
                //新需求  如果param.getValue()获取的值大于多少截取4位不为0000 ，则向fn_warm表中添加数据
                String value = param.getValue();
                if (value.length()>42){
                    String subStr = value.substring(38,42);
                    if(!subStr.equals("0000")){
                        Warm warm = new Warm();
                        warm.setArgs(value);
                        warm.setProId(param.getUser_id());
                        warm.setDevId(param.getDev_id());
                        warm.setWarnTime(dateFormat.parse(param.getAt()));
                        warmService.insert(warm);
                    }
                }
            }
            result.success("修改成功");
        } catch (Exception e) {
            log.debug(e.getMessage());
            result.fail(null, e.getMessage());
        }
        return result;
    }

    @ApiOperation("在线设备个数统计")
    @PostMapping("/getOnlineNumber")
    public Object getOnlineNumber() {
        PageVo pageVo = new PageVo();
        Map<String, Object> map = new HashMap<String, Object>();
        map = service.getOnlineCount(map);
        map.put("status", 0);
        map.put("message", "查询成功");
        return map;
    }
}

